package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.util.AliyunOSSUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 11:41
 */
@Service
public class CategoryServiceImpl implements  CategoryService {

    @Autowired
    private DtsCategoryMapper mapper;


    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    //桶名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;
    @Override
    public IPage<DtsCategory> list(Integer id, String name, Integer page, Integer limit, String sort, String order) {

        IPage<DtsCategory> objectPage = new Page<>(page,limit);

        QueryWrapper<DtsCategory> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("deleted", false);
        if(!StringUtils.isEmpty(id)){
             objectQueryWrapper.eq("id", id);
        }
        if(!StringUtils.isEmpty(name)){
            objectQueryWrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(sort)&& !StringUtils.isEmpty(order)){

            if(order.equalsIgnoreCase("asc")){
                objectQueryWrapper.orderByAsc();
            }else {
                objectQueryWrapper.orderByDesc();
            }
        }
        IPage<DtsCategory> dtsCategoryIPage = mapper.selectPage(objectPage, objectQueryWrapper);

        return dtsCategoryIPage;
    }

    @Override
    public List<CatVo> list1() {
        List<CatVo> list = mapper.list();
        return list;
    }

    /**
     * 添加
     * @param category
     * @return
     */
    @Override
    public DtsCategory add(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        int insert = mapper.insert(category);
        return category;
    }

    /**
     * 修改
     * @param dtsCategory
     * @return
     */
    @Override
    public int update(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        int i = mapper.updateById(dtsCategory);

        return i;
    }

    /**
     * 删除
     * @param dtsCategory
     * @return
     */
    @Override
    public int delete(DtsCategory dtsCategory) {

        String picUrl = dtsCategory.getPicUrl();
        String level = dtsCategory.getIconUrl();
        if(!StringUtils.isEmpty(picUrl)){
            int i = picUrl.lastIndexOf("/");
            int i1 = picUrl.lastIndexOf("?");
            String substring = picUrl.substring(i + 1, i1);
            AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, substring);
        }
        if(!StringUtils.isEmpty(level)){
            int i2 = level.lastIndexOf("/");
            int i3 = level.lastIndexOf("?");
            String str=level.substring(i2+1,i3);
            AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, str);
        }

        Integer id = dtsCategory.getId();
        QueryWrapper<DtsCategory> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",id);
        //删除数据库
        int delete = mapper.delete(objectQueryWrapper);
        return delete;
    }

    /**
     *下拉表
     * @return
     */
    @Override
    public List<DtsCategory> list() {
       /* QueryWrapper<DtsCategory> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("deleted", false);*/
        List<DtsCategory> dtsCategories = mapper.selectList(null);
        return dtsCategories;
    }
}
